package com.zqw.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqw.info.AuthInfo;
import com.zqw.info.ResponseMsg;
import com.zqw.pojo.AuthPojo;
import com.zqw.service.AuthService;
import com.zqw.util.MyStaticFinal;
import com.zqw.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AuthController {

    @Autowired
    AuthService authService;

    //获取所有的权限信息
    @RequestMapping("queryAuth")
    @ResponseBody
    public List<AuthPojo> queryAuth(HttpServletRequest request){
        List<AuthPojo> authPojos = authService.queryAuth(new AuthInfo());
        return authPojos;
    }

    //转发到权限管理界面
    @RequestMapping("adminAuth")
    public String adminAuth(AuthInfo authInfo,HttpServletRequest request){
        PageHelper.startPage(authInfo.getPageNum(),authInfo.getPageSize());
        List<AuthPojo> authPojos = authService.queryAuthByType("2");
        PageInfo<AuthPojo> list = new PageInfo<>(authPojos);
        request.setAttribute("pageInfo",list);
        return "admin-rule";
    }

    //转发到权限分类界面
    @RequestMapping("adminCate")
    public String adminCate(HttpServletRequest request){

        return "goods-list";
    }

    //根据id删除权限
    @RequestMapping("delAuth")
    @ResponseBody
    public ResponseMsg delAuth(String id){
        boolean b = authService.delOneAuth(id);
        ResponseMsg responseMsg = new ResponseMsg();
        if(b){
            responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
        }else{
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("删除失败");
        }
        return responseMsg;
    }

    //根据一组id删除管理员
    @RequestMapping("delAuths")
    @ResponseBody
    public ResponseMsg delAuths(String ids){
        ResponseMsg responseMsg = new ResponseMsg();
        if(StringUtil.isEmpty(ids)) {
            responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
            responseMsg.setResultMsg("删除失败");
        }else{
            boolean b = authService.delAuths(ids);
            if (b) {
                responseMsg.setResult(MyStaticFinal.SUCCESS_RESULT_ADMIN);
            } else {
                responseMsg.setResult(MyStaticFinal.ERROR_RESULT_ADMIN);
                responseMsg.setResultMsg("删除失败");
            }
        }
        return responseMsg;
    }
}
